package com.haoshuang.zhouzhoubang.ui.page.account.forget

import android.content.Context
import android.os.Bundle
import com.haoshuang.zhouzhoubang.databinding.ActivityForgetBinding
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.CountDownWorker
import com.mufeng.libs.utils.ktx.hidePassword
import com.mufeng.libs.utils.ktx.showPassword
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.view.clickWithTrigger


/**
 *  找回密码
 */
class ForgetActivity : BaseVMActivity<ForgetViewModel, ActivityForgetBinding>() {

    companion object {
        fun launch(context: Context) {
            context.startPage<ForgetActivity>()
        }
    }

    override fun initView(savedInstanceState: Bundle?) {

        binding.titleBar.title = "忘记密码"
        binding.titleBar.leftImageView().click { finish() }

        binding.tvGetCode.clickWithTrigger {
            val phone = binding.etPhone.text.toString()
            if (phone.isEmpty()) {
                toast("请输入手机号")
                return@clickWithTrigger
            }
            if (phone.length != 11) {
                toast("请输入正确的手机号")
                return@clickWithTrigger
            }
            vm.getCode(phone)
        }

        binding.cbPassword.setOnCheckedChangeListener { buttonView, isChecked ->
            if (isChecked) {
                binding.etPassword.showPassword()
            } else {
                binding.etPassword.hidePassword()
            }
        }

        binding.cbPasswordAgain.setOnCheckedChangeListener { buttonView, isChecked ->
            if (isChecked) {
                binding.etPasswordAgain.showPassword()
            } else {
                binding.etPasswordAgain.hidePassword()
            }
        }

        binding.tvSave.clickWithTrigger {
            val phone = binding.etPhone.text.toString()
            val code = binding.etCode.text.toString()
            val password = binding.etPassword.text.toString()
            val passwordAgain = binding.etPasswordAgain.text.toString()
            if (phone.isEmpty()) {
                toast("请输入手机号")
                return@clickWithTrigger
            }
            if (phone.length != 11) {
                toast("请输入正确的手机号")
                return@clickWithTrigger
            }
            if (code.isEmpty()) {
                toast("请输入验证码")
                return@clickWithTrigger
            }
            if (password.isEmpty()) {
                toast("请输入密码")
                return@clickWithTrigger
            }
            if (passwordAgain.isEmpty()) {
                toast("请输入确认密码")
                return@clickWithTrigger
            }
            if (password != passwordAgain) {
                toast("两次密码不一致")
                return@clickWithTrigger
            }
            vm.forget(phone, code, password)

        }
    }

    override fun initData() {

    }

    override fun startObserve() {
        vm.getCodeData.observeState(this){
            onLoading {
                showLoading()
            }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                toast("验证码已发送")
                binding.tvGetCode.isEnabled = false
                CountDownWorker(this@ForgetActivity, onChange = {
                    binding.tvGetCode.text = "${it}S"
                }, onFinish = {
                    binding.tvGetCode.isEnabled = true
                    binding.tvGetCode.text = "重新获取"
                }).start()
            }
        }

        vm.forgetData.observeState(this){
            onLoading {
                showLoading()
            }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                finish()
            }
        }
    }

}